package com.qunar.mina.app.handler;

import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.util.NumericUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qunar.mina.app.service.IndexService;
import com.qunar.mina.app.service.SearchService;
import com.qunar.mina.app.service.impl.Indexer;
import com.qunar.mina.app.service.impl.Searcher;
import com.qunar.qcf.handler.InvokeHandler;
import com.qunar.qcf.handler.annotation.Handler;
import com.qunar.qcf.handler.annotation.Remote;
import com.qunar.wireless.ugc.qcfclient.dto.Address;
import com.qunar.wireless.ugc.qcfclient.dto.Album;
import com.qunar.wireless.ugc.qcfclient.dto.Query;
import com.qunar.wireless.ugc.qcfclient.dto.QueryResult;
import com.qunar.wireless.ugc.qcfclient.dto.Record;
import com.qunar.wireless.ugc.qcfclient.dto.User;

@Handler(name = "addresshandler")
public class SearcherHandler implements InvokeHandler {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private SearchService searcher = new Searcher();

	private IndexService indexer = new Indexer();
	private final String ADDRESS = "address";
	private final String REC = "record";
	private final String USER = "user";
	private final String AUDITOR = "auditor";
	private final String ALBUM = "album";

	/** -----------------------查询操作相关接口-------------------------- */

	/** 目的地查询 */
	@Remote(url = "getDestin")
	public QueryResult getDestinResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.POISearcher(query.getKeyword(), true,
				query.getFirst(), query.getMax(), ADDRESS, query.getFields());

		return qr;
	}

	/** POI查询 */
	@Remote(url = "getPOI")
	public QueryResult getPOIResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.POISearcher(query.getKeyword(), false,
				query.getFirst(), query.getMax(), ADDRESS, query.getFields());

		return qr;
	}

	/** 通用地址查询查询 */
	@Remote(url = "getquery")
	public QueryResult getQueryResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.searcher(query.getKeyword(),
				query.getFirst(), query.getMax(), ADDRESS, query.getFields());

		return qr;
	}
	
	/** 通用地址查询查询 */
	@Remote(url = "getboostquery")
	public QueryResult getBoostQueryResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.boostSearcher(query.getKeyword(),
				query.getFirst(), query.getMax(), ADDRESS,query.getBoosts(), query.getFields());

		return qr;
	}

	/** 图片查询 */
	@Remote(url = "getpic")
	public QueryResult getPicResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.searcher(query.getKeyword(),
				query.getFirst(), query.getMax(), REC, query.getFields());
		return qr;
	}

	/** 用户查询 */
	@Remote(url = "getuser")
	public QueryResult getUserResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.searcher(query.getKeyword(),
				query.getFirst(), query.getMax(), USER, query.getFields());
		return qr;
	}

	/** 操作员查询 */
	@Remote(url = "getauditor")
	public QueryResult getAuditorResult(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.searcherScan(query.getKeyword(),
				query.getFirst(), query.getMax(), AUDITOR, query.getFields());
		return qr;
	}

	/** 用户名模糊查询 */
	@Remote(url = "fuzzyuser")
	public QueryResult getFuzzyUser(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");

			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.fuzzySearcher(query.getKeyword(),
				query.getFirst(), query.getMax(), query.getFields()[0]);
		return qr;
	}

	/** 用户名符查询查询 */
	@Remote(url = "wildcarduser")
	public QueryResult getWildCardUser(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.wildcardSearcher(query.getKeyword(),
				query.getFirst(), query.getMax(), query.getFields()[0]);
		return qr;
	}

	/** 查询album */
	@Remote(url = "getAlbum")
	public QueryResult getAlbum(Query query) {
		if (null == query || query.getKeyword() == null) {
			logger.error("query or keywork is null");
			return null;
		}
		System.out.println("query" + query.toString());
		QueryResult qr = searcher.searcher(query.getKeyword(),
				query.getFirst(), query.getMax(), ALBUM, "name");
		return qr;
	}

	/** ----------------------- 地點索引相关接口-------------------------- */
	/** 创建全部索引 */
	@Remote(url = "createindex")
	public String createIndex() {
		indexer.createAllIndex();
		return "create index success";
	}

	/** 删除地址索引 */
	@Remote(url = "delAddrIndex")
	public String delAddrIndex(Address a) {
		if (a == null || a.getAddressId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}
		Term term = new Term("addressId", NumericUtils.longToPrefixCoded(a
				.getAddressId()));
		indexer.delIndex(ADDRESS, term);
		return "delete address index success";
	}

	//
	/** 批量删除地址索引 */
	@Remote(url = "batchDelAddrIndex")
	public String batchDelAddrIndex(List<Address> list) {
		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object or objectId is null";

		}
		Term[] terms = new Term[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			if (o instanceof Address) {
				Address a = (Address) o;
				terms[i] = new Term("addressId",
						NumericUtils.longToPrefixCoded(a.getAddressId()));
			}
		}

		indexer.batchDelIndex(ADDRESS, terms);
		return "batch delete address index success";
	}

	/** 插入地址索引 */
	@Remote(url = "insertAddrIndex")
	public String insertAddrIndex(Address a) {

		if (a == null || a.getAddressId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}
		indexer.addIndex(a, ADDRESS);
		return "insert address index success";
	}

	/** 批量插入地址索引 */
	@Remote(url = "batchInsertAddrIndex")
	public String batchInsertAddrIndex(List<Address> list) {
		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}
		indexer.batchAddIndex(list, ADDRESS);
		return "batch insert address index success";
	}

	/** 更新地址索引 */
	@Remote(url = "updateAddrIndex")
	public String updateAddrIndex(Address a) {
		if (a == null || a.getAddressId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		Term term = new Term("addressId", NumericUtils.longToPrefixCoded(a
				.getAddressId()));
		indexer.updateIndex(a, ADDRESS, term);
		return "update address index success";
	}

	/** 批量更新地址索引 */
	@Remote(url = "batchUpdateAddrIndex")
	public String batchUpdateAddrIndex(List<?> list) {

		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}

		Term[] terms = new Term[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			if (o instanceof Address) {
				Address a = (Address) o;
				terms[i] = new Term("addressId",
						NumericUtils.longToPrefixCoded(a.getAddressId()));
			}
		}
		indexer.batchUpdateIndex(list, ADDRESS, terms);
		return "batch update address index success";
	}

	/** --------Record索引相关操作相关操作---------- */

	/** 插入Record索引 */
	@Remote(url = "insertRecIndex")
	public String insertRecIndex(Record r) {

		if (r == null || r.getId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		indexer.addIndex(r, REC);
		return "insert record index success";
	}

	/** 批量插入Record索引 */
	@Remote(url = "batchInsertRecIndex")
	public String batchInsertRecIndex(List<Record> list) {

		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}

		indexer.batchAddIndex(list, REC);
		return "batch insert record index success";
	}

	/** 更新Record索引 */
	@Remote(url = "updateRecIndex")
	public String updateRecIndex(Record r) {

		if (r == null || r.getId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		Term term = new Term("id", NumericUtils.longToPrefixCoded(r.getId()));
		indexer.updateIndex(r, REC, term);
		return "update record index success";
	}

	/** 批量更新Record索引 */
	@Remote(url = "batchUpdateRecIndex")
	public String batchUpdateRecIndex(List<?> list) {

		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}

		Term[] terms = new Term[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			if (o instanceof Record) {
				Record r = (Record) o;
				terms[i] = new Term("id", NumericUtils.longToPrefixCoded(r
						.getId()));
			}
		}
		indexer.batchUpdateIndex(list, REC, terms);
		return "batch update record index success";
	}

	/** 删除Record索引 */
	@Remote(url = "delRecIndex")
	public String delRecIndex(Record r) {

		if (r == null || r.getId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		Term term = new Term("id", NumericUtils.longToPrefixCoded(r.getId()));
		indexer.delIndex(REC, term);
		return "delete address index success";
	}

	/** 批量删除Record索引 */
	@Remote(url = "batchDelRecIndex")
	public String batchDelRecIndex(List<Record> list) {

		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}

		Term[] terms = new Term[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			if (o instanceof Record) {
				Record r = (Record) o;
				terms[i] = new Term("id", NumericUtils.longToPrefixCoded(r
						.getId()));
			}
		}

		indexer.batchDelIndex(REC, terms);
		return "batch delete record index success";
	}

	/** 添加User索引 */
	@Remote(url = "insertUserIndex")
	public String insertUserIndex(User u) {

		if (u == null || u.getUserId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		indexer.addIndex(u, USER);
		return "insert user index success";
	}

	@Remote(url = "updateUserIndex")
	public String updateUserIndex(User u) {

		if (u == null || u.getUserId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		Term term = new Term("userId", NumericUtils.longToPrefixCoded(u
				.getUserId()));
		indexer.updateIndex(u, USER, term);
		return "update user index success";
	}

	/** --------------Album 相关操作 --------------- */

	/** 添加Album索引 */
	@Remote(url = "insertAlbumIndex")
	public String insertAlbumIndex(Album a) {

		if (a == null || a.getId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		indexer.addIndex(a, ALBUM);
		return "insert album index success";
	}

	/** 更新Album索引 */
	@Remote(url = "updateAlbumIndex")
	public String updateAlbumIndex(Album a) {

		if (a == null || a.getId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		Term term = new Term("id", NumericUtils.longToPrefixCoded(a.getId()));
		indexer.updateIndex(a, ALBUM, term);
		return "update album index success";
	}

	/** 批量更新album索引 */
	@Remote(url = "batchUpdateAlbumIndex")
	public String batchUpdateAlbumIndex(List<?> list) {

		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}

		Term[] terms = new Term[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			if (o instanceof Album) {
				Album a = (Album) o;
				terms[i] = new Term("id", NumericUtils.longToPrefixCoded(a
						.getId()));
			}
		}
		indexer.batchUpdateIndex(list, ALBUM, terms);
		return "batch update album index success";
	}

	/** 删除Album索引 */
	@Remote(url = "delAlbumIndex")
	public String delAlbumIndex(Album a) {

		if (a == null || a.getId() == null) {
			logger.error("Argument object or objectId is null");
			return "Argument object or objectId is null";
		}

		Term term = new Term("id", NumericUtils.longToPrefixCoded(a.getId()));
		indexer.delIndex(ALBUM, term);
		return "delete album index success";
	}

	/** 批量删除Album索引 */
	@Remote(url = "batchDelAlbumcIndex")
	public String batchDelAlbumIndex(List<Album> list) {

		if (list == null || list.size() == 0) {
			logger.error("Argument object is null or empty");
			return "Argument object is null or empty";
		}

		Term[] terms = new Term[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			if (o instanceof Album) {
				Album a = (Album) o;
				terms[i] = new Term("id", NumericUtils.longToPrefixCoded(a
						.getId()));
			}
		}

		indexer.batchDelIndex(ALBUM, terms);
		return "batch delete album index success";
	}
}
